package com.company;

import java.util.ArrayList;
import java.util.Collections;

/*test*/

public class PlayRoom extends Thread {
    public static int incTime;
    public static int[] pos;
    public static ArrayList<Integer> resultSet = new ArrayList<>();
    public Ant[] ant;

    public PlayRoom(Ant[] ant) {
        this.ant = ant;
        setIncTime(1);
        setPos(new int[]{30, 80, 110, 160, 250});
    }

    public static void setIncTime(int incTime) {
        PlayRoom.incTime = incTime;
    }

    public static void setPos(int[] pos) {
        PlayRoom.pos = pos;
    }

    public void run() {
        playGames();
    }

    public void buildDirections(Ant[] ant, int i) {
        int k = i;
        for (int j = 0; j < 5; j++) {
            ant[j] = new Ant();
            ant[j].setId(j);
            ant[j].setDirection(k % 2);
            ant[j].setPosition(pos[j]);
            k /= 2;
        }
    }

    public int getMaxTime(Ant[] ant) {
        int maxTime = 0;
        for (Ant a : ant) {
            if (maxTime < a.getLeftTime())
                maxTime = a.getLeftTime();
        }
        return maxTime;
    }

    public void setResultSet() {
        resultSet.add(getMaxTime(ant));
    }

    public void printResult() {
        StringBuilder result = new StringBuilder("Result: ");
        StringBuilder finishTime = new StringBuilder("Last-time: ");
        StringBuilder MAX = new StringBuilder("MAX: ");
        StringBuilder MIN = new StringBuilder("MIN: ");

        for (Ant a : ant) {
            result.append(a.getLeftTime());
            result.append(" ");
        }
        finishTime.append(getMaxTime(ant));
        Collections.sort(resultSet);
        MIN.append(resultSet.get(0));
        MAX.append(resultSet.get(resultSet.size() - 1));

        Gui.l3.setText(result.toString());
        Gui.l4.setText(finishTime.toString());
        Gui.l6.setText(MAX.toString());
        Gui.l7.setText(MIN.toString());
    }

    public void playGames() {
        CreepingGame creepingGame = new CreepingGame();
        creepingGame.playGame(ant);
        setResultSet();
        printResult();
    }
}
